package com.compass.mobile.framework.helper

import LogUtil
import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import androidx.multidex.MultiDex
import com.compass.mobile.framework.constant.NIM_APP_KEY
import com.compass.mobile.framework.ifly.IFlyHelper
import com.compass.mobile.framework.ifly.IFlyOptCallback
import com.compass.mobile.framework.utils.TipsToast
import com.netease.nimlib.sdk.NIMClient
import com.netease.nimlib.sdk.SDKOptions
import com.tencent.mmkv.MMKV
import com.tencent.mmkv.MMKVLogLevel

/**
 * @desc   提供应用环境
 */
@SuppressLint("StaticFieldLeak")
object CompassAppHelper {
    private lateinit var app: Application
    private var isDebug = false
    private lateinit var mContext :Context

    /**
     * @param application  application对象
     * @param isDebug   是否是debug模式
     */
    fun init(application: Application, isDebug: Boolean) {
        this.app = application
        this.isDebug = isDebug
        AppManager.init(application)
        MultiDex.install(application)
        initMVVK(application)
        TipsToast.init(application)
        initNIMClient(application)
//        IFlyHelper.initIFly(application,object : IFlyOptCallback{
//            override fun onSuccess(text: String?) {
//                if(isDebug){
//                    LogUtil.d("init 成功")
//                }
//            }
//
//            override fun onFail(code: Int, text: String?) {
//                if(isDebug){
//                    LogUtil.d("init 失败")
//                }
//            }
//        })
    }

    private fun initMVVK(application: Application) {
        MMKV.initialize(application)
        MMKV.setLogLevel(
            if (isDebug) {
                MMKVLogLevel.LevelDebug
            } else {
                MMKVLogLevel.LevelError
            }
        )
    }

    private fun getSDKOptions(appKey: String?): SDKOptions {
        val options = SDKOptions()
        options.appKey = appKey
        options.preloadAttach = true
        options.disableAwake = true
        options.sessionReadAck = true
        options.animatedImageThumbnailEnabled = true
        options.asyncInitSDK = true
        options.reducedIM = false
        //是否在 SDK 初始化时检查 Manifest 文件配置是否完成，默认为 false，不检查。
        //par使用建议： 建议在调试阶段开启，上线时关闭
        options.checkManifestConfig = true
        options.enableTeamMsgAck = true
        return options
    }


    /**
     * 初始化IM_SDK
     */
    private fun initNIMClient(application: Application) {
        val options = getSDKOptions(NIM_APP_KEY)
        NIMClient.init(application, null, options)

    }
    /**
     * 获取全局应用
     */
    fun getApplication() = app

    /**
     * 是否为debug环境
     */
    fun isDebug() = isDebug

    fun release(){
        IFlyHelper.unInitIFly()
    }
}